package wtc.stu.dao;

import wtc.stu.entity.StuEntity;
import wtc.stu.util.DBConnection;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class StuDaoScore {
    private DBConnection dbc;
    private Connection conn = null;
    private ResultSet rs = null;
    private PreparedStatement pre = null;
    public StuDaoScore()
    {
        dbc = new DBConnection();

    }
    // 全查询分数信息
    public List<StuEntity> getAll()
    {
        List<StuEntity> ar = new ArrayList<StuEntity>();
        try
        {
            conn = dbc.getConnection();
            String sql = "select * from scoreinfo";
            pre = conn.prepareStatement(sql);
            rs = pre.executeQuery();
            while (rs.next())
            {
                StuEntity score = new StuEntity();
                score.setScoreId(rs.getInt("scoreId"));
                score.setScore(rs.getInt("score"));
                score.setSid(rs.getInt("sid"));
                score.setScoreRemark(rs.getString("scoreRemark"));
                ar.add(score);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            dbc.closeAll(pre, conn, rs);
        }
        return ar;
    }
    // 查询一个分数信息表对象
    public StuEntity getOneById(int scoreId)
    {
        StuEntity score = new StuEntity();

        try
        {
            conn = dbc.getConnection();
            String sql = "select * from scoreinfo where scoreId=?";
            pre = conn.prepareStatement(sql);
            pre.setInt(1, scoreId);
            rs = pre.executeQuery();
            while (rs.next())
            {
                score.setScoreId(rs.getInt("scoreId"));
                score.setScore(rs.getInt("score"));
                score.setSid(rs.getInt("sid"));
                score.setScoreRemark(rs.getString("scoreRemark"));
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            dbc.closeAll(pre, conn, rs);
        }
        return score;
    }
    // 增加分数信息表对象
    public void addStu(StuEntity score)
    {
        try
        {
            conn = dbc.getConnection();
            String sql = "insert into scoreinfo values(null,?,?,?)";
            pre = conn.prepareStatement(sql);
            pre.setInt(1,score.getScore());
            pre.setInt(2, score.getSid());
            pre.setString(3, score.getScoreRemark());
            pre.executeUpdate();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            dbc.closeAll(pre, conn, rs);
        }
    }
    // 修改分数表对象
    public void updateStu(StuEntity score)
    {
        try
        {
            conn = dbc.getConnection();
            String sql = "update  scoreinfo  set score=?,sid=?, scoreRemark=? where scoreId=?";
            pre = conn.prepareStatement(sql);
            pre.setInt(1, score.getScore());
            pre.setInt(2, score.getSid());
            pre.setString(3, score.getScoreRemark());
            pre.setInt(4, score.getScoreId());
            pre.executeUpdate();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            dbc.closeAll(pre, conn, rs);
        }
    }
    // 删除分数表对象
    public void delStu(int scoreId)
    {
        try
        {
            conn = dbc.getConnection();
            String sql = "delete from scoreinfo where scoreId=?";
            pre = conn.prepareStatement(sql);
            pre.setInt(1,scoreId);
            pre.executeUpdate();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            dbc.closeAll(pre, conn, rs);
        }
    }
}
